# 1.读取多种数据格式文件（csv，xlsx，txt），使用merge进行合并，并将合并数据分别保存成csv, xlsx, txt
格式文件。

library(openxlsx)
ADdata1<-read.delim("C:/Users/32076/Desktop/文件数据/ADdata1.txt")
ADdata2<-read.csv("C:/Users/32076/Desktop/文件数据/ADdata2.csv",row.names = 1)
ADdata3 <- read.xlsx("C:/Users/32076/Desktop/文件数据/ADdata3.xlsx",rowNames = TRUE)
ADdata4<-read.table("C:/Users/32076/Desktop/文件数据/ADdata4.txt")
merged_data <- merge(ADdata1, ADdata2,by.y = x)
ADdata5<-merge(ADdata1,ADdata2,by = 0) #1,2合并 根据行名合并写法1
ADdata6<-merge(ADdata3,ADdata4, by = "row.names") #3,4合并 根据行名合并写法2
ADdata <- merge(ADdata5,ADdata6) #最终合并
write.csv(ADdata,file="C:/Users/32076/Desktop/文件数据/ADdata.csv") #保存为csv文件
write.xlsx(ADdata,file="C:/Users/32076/Desktop/文件数据/ADdata.xlsx") #保存为xlsx文件
write.table(ADdata,file="C:/Users/32076/Desktop/文件数据/ADdata.txt") #保存为txt文件



# 1.读取clin_inf，exp_inf，ID_annoation，count 4个文件，将其进行整理，最终存为ExpressionSet格式数据
count <- read.table("C:/Users/32076/Desktop/实践3文件数据2/count.csv",header = T)
clin <- read.table("C:/Users/32076/Desktop/实践3文件数据2/clin_inf.csv",header = T)
exp <- read.table("C:/Users/32076/Desktop/实践3文件数据2/exp_inf.csv",header = T)
IDanno <- read.table("C:/Users/32076/Desktop/实践3文件数据2/ID_annoation.csv",header = T)
meta <- merge(clin,exp)
rownames(count) <- count$gene_id
count <- count[,-1]
library(Biobase)
x <- colnames(count)
x <- gsub("(.*).$","\\1",x)
x <- gsub("X(...$)","PTB\\1",x)
x <- gsub("X","XYA",x)
colnames(count) <- x
rownames(meta) <- meta$样本名称
meta <- meta[match(colnames(count),rownames(meta)),]
rownames(IDanno) <- IDanno$gene_id
IDanno$gene_id <- gsub("\\..*","",IDanno$gene_id)
pos2 <- duplicated(IDanno$gene_id)
IDanno2 <- IDanno[IDanno$gene_id %in% IDanno$gene_id[pos2],]
IDanno <- IDanno[!duplicated(IDanno$gene_id),]
rownames(IDanno) <- IDanno$gene_id
IDanno <- IDanno[match(rownames(count),rownames(IDanno)),]
# 创建 ExpressionSet 对象
eset <- ExpressionSet(assayData = as.matrix(count),
                      phenoData = new("AnnotatedDataFrame", data = meta),
                      featureData = new("AnnotatedDataFrame", data = IDanno))
save(eset, file = "C:/Users/32076/Desktop/实践3文件数据2/RNAseq.csv")

